package com.ljx.recruitment.user.controller;

import com.ljx.recruitment.common.domain.vo.resp.ApiResult;
import com.ljx.recruitment.common.domain.vo.resp.PageResp;
import com.ljx.recruitment.security.annotation.ApiPermission;
import com.ljx.recruitment.security.common.PermissionConstant;
import com.ljx.recruitment.user.domain.entity.Role;
import com.ljx.recruitment.user.domain.vo.req.RolePageReq;
import com.ljx.recruitment.user.domain.vo.req.RoleSubmitReq;
import com.ljx.recruitment.user.domain.vo.req.RoleToUserReq;
import com.ljx.recruitment.user.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-10-31  13:43
 */
@Api(tags = "角色接口")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiPermission(permissions = {PermissionConstant.ROLE_QUERY})
    @ApiOperation("获取角色分页")
    @GetMapping("/page")
    public ApiResult<PageResp<Role>> getPage(RolePageReq req) {
        return ApiResult.success(roleService.getPage(req));
    }

    @ApiPermission(permissions = {PermissionConstant.ROLE_SUBMIT})
    @ApiOperation("创建/修改角色")
    @PostMapping("/submit")
    public ApiResult<Role> submit(@Validated @RequestBody RoleSubmitReq req){
        return ApiResult.success(roleService.submit(req));
    }

    @ApiPermission(permissions = {PermissionConstant.ROLE_SUBMIT, PermissionConstant.ADMIN_USER_SUBMIT})
    @ApiOperation("给用户分配角色")
    @PostMapping("/toUser")
    public ApiResult<Void> allocationToUser(@Validated @RequestBody RoleToUserReq req){
        roleService.allocationToUser(req);
        return ApiResult.success();
    }

    @ApiPermission(permissions = {PermissionConstant.PERMISSION_SUBMIT})
    @ApiOperation("获取角色权限")
    @GetMapping("/permission/{roleId}")
    public ApiResult<List<Long>> getPermissions(@PathVariable("roleId") Long roleId) {
        return ApiResult.success(roleService.getPermissions(roleId));
    }

    @ApiPermission(permissions = {PermissionConstant.MENU_QUERY})
    @ApiOperation("获取角色菜单")
    @GetMapping("/menu/{roleId}")
    public ApiResult<List<Long>> getMenus(@PathVariable("roleId") Long roleId) {
        return ApiResult.success(roleService.getMenus(roleId));
    }


}
